﻿///Danh sách các thư viện sử dụng, có thêm thư viện System.Media
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Media;
///Thân Project
namespace Calculator
{
    /// <summary>
    /// Lớp chính
    /// </summary>
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        /// <summary>
        /// Khai báo các biến sử dụng
        /// </summary>
        int clear = 1; decimal result, mresult = 0; string op;
        /// <summary>
        /// Phương thức hiển thị màn hình Calculator
        /// </summary>
        /// <param name="cl"></param>
        /// <returns></returns>
        int Clear(int cl)
        {
            switch (cl)
            {
                case 1:
                    {
                        label1.Text = "";
                    } break;
                case 2:
                    {
                        label1.Text = ""; label2.Text = ""; op = "";
                    } break;
                case 3:
                    {
                        label1.Text = ""; label2.Text = label2.Text.Remove(label2.Text.IndexOf('M'));
                    } break;
                case 4:
                    {
                        label1.Text = ""; label2.Text = label2.Text.Remove(label2.Text.IndexOf('s'));
                    } break;
                case 5:
                    {
                        label1.Text = ""; label2.Text = label2.Text.Remove(label2.Text.LastIndexOf(' ') + 1);
                    } break;
            }
            return 0;
        }
        /// <summary>
        /// Phương thức các phép tính +, -, *, /, Mod
        /// </summary>
        /// <param name="op"></param>
        /// <returns></returns>
        decimal eval(string op)
        {
            clear = 1;
            try
            {
                switch (op)
                {
                    case "+": result = result + Convert.ToDecimal(label1.Text); break;
                    case "-": result = result - Convert.ToDecimal(label1.Text); break;
                    case "*": result = result * Convert.ToDecimal(label1.Text); break;
                    case "/": result = result / Convert.ToDecimal(label1.Text); break;
                    case "Mod": result = result % Convert.ToDecimal(label1.Text); break;
                    default: result = Convert.ToDecimal(label1.Text); break;
                }
            }
            catch (System.OverflowException) { label2.Text = ""; label2.Text = "Tran So"; clear = 2; SystemSounds.Asterisk.Play(); }
            catch (System.DivideByZeroException) { label2.Text = ""; label2.Text = "Khong the chia cho 0"; clear = 2; SystemSounds.Asterisk.Play(); }
            return result;
        }
        /// <summary>
        /// Phương thức khi ấn phím 0
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button0_Click(object sender, EventArgs e)
        {
            clear = Clear(clear);
            if (label1.Text.Length < 21)
                label1.Text = label1.Text + "0";
            else SystemSounds.Beep.Play();
        }
        /// <summary>
        /// Phương thức khi ấn phím 1
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            clear = Clear(clear);
            if (label1.Text.Length < 21)
                label1.Text = label1.Text + "1";
            else SystemSounds.Beep.Play();
        }
        /// <summary>
        /// Phương thức khi ấn phím 2
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            clear = Clear(clear);
            if (label1.Text.Length < 21)
                label1.Text = label1.Text + "2";
            else SystemSounds.Beep.Play();
        }
        /// <summary>
        /// Phương thức khi ấn phím 3
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button3_Click(object sender, EventArgs e)
        {
            clear = Clear(clear);
            if (label1.Text.Length < 21)
                label1.Text = label1.Text + "3";
            else SystemSounds.Beep.Play();
        }
        /// <summary>
        /// Phương thức khi ấn phím 4
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button4_Click(object sender, EventArgs e)
        {
            clear = Clear(clear);
            if (label1.Text.Length < 21)
                label1.Text = label1.Text + "4";
            else SystemSounds.Beep.Play();
        }
        /// <summary>
        /// Phương thức khi ấn phím 5
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button5_Click(object sender, EventArgs e)
        {
            clear = Clear(clear);
            if (label1.Text.Length < 21)
                label1.Text = label1.Text + "5";
            else SystemSounds.Beep.Play();
        }
        /// <summary>
        /// Phương thức khi ấn phím 6
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button6_Click(object sender, EventArgs e)
        {
            clear = Clear(clear);
            if (label1.Text.Length < 21)
                label1.Text = label1.Text + "6";
            else SystemSounds.Beep.Play();
        }
        /// <summary>
        /// Phương thức khi ấn phím 7
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button7_Click(object sender, EventArgs e)
        {
            clear = Clear(clear);
            if (label1.Text.Length < 21)
                label1.Text = label1.Text + "7";
            else SystemSounds.Beep.Play();
        }
        /// <summary>
        /// Phương thức khi ấn phím 8
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button8_Click(object sender, EventArgs e)
        {
            clear = Clear(clear);
            if (label1.Text.Length < 21)
                label1.Text = label1.Text + "8";
            else SystemSounds.Beep.Play();
        }
        /// <summary>
        /// Phương thức khi ấn phím 9
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button9_Click(object sender, EventArgs e)
        {
            clear = Clear(clear);
            if (label1.Text.Length < 21)
                label1.Text = label1.Text + "9";
            else SystemSounds.Beep.Play();
        }
        /// <summary>
        /// Phương thức khi ấn phím Chấm
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonCham_Click(object sender, EventArgs e)
        {
            if (label1.Text.Contains('.'))
            {
                SystemSounds.Beep.Play();
            }
            else label1.Text = label1.Text + ".";
        }
        /// <summary>
        /// Phương thức khi ấn phím Cộng
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonCong_Click(object sender, EventArgs e)
        {
            if (clear == 3 || clear == 4 || clear == 5)
                label2.Text = label2.Text + " + ";
            else label2.Text = label2.Text + label1.Text + " + ";
            label1.Text = eval(op).ToString();
            op = "+";
        }
        /// <summary>
        /// Phương thức khi ấn phím Trừ
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonTru_Click(object sender, EventArgs e)
        {
            if (clear == 3 || clear == 4 || clear == 5)
                label2.Text = label2.Text + " - ";
            else label2.Text = label2.Text + label1.Text + " - ";
            label1.Text = eval(op).ToString();
            op = "-";
        }
        /// <summary>
        /// Phương thức khi ấn phím Nhân
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonNhan_Click(object sender, EventArgs e)
        {
            if (clear == 3 || clear == 4 || clear == 5)
                label2.Text = label2.Text + " * ";
            else label2.Text = label2.Text + label1.Text + " * ";
            label1.Text = eval(op).ToString();
            op = "*";
        }
        /// <summary>
        /// Phương thức khi ấn phím Chia
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonChia_Click(object sender, EventArgs e)
        {
            if (clear == 3 || clear == 4 || clear == 5)
                label2.Text = label2.Text + " / ";
            else label2.Text = label2.Text + label1.Text + " / ";
            label1.Text = eval(op).ToString();
            op = "/";
        }
        /// <summary>
        /// Phương thức khi ấn phím Mod
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonMod_Click(object sender, EventArgs e)
        {
            if (clear == 3 || clear == 4 || clear == 5)
                label2.Text = label2.Text + " Mod ";
            else label2.Text = label2.Text + label1.Text + " Mod ";
            label1.Text = eval(op).ToString();
            op = "Mod";
        }
        /// <summary>
        /// Phương thức khi ấn phím 1 trên X
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1trenX_Click(object sender, EventArgs e)
        {
            if (label2.Text.Contains("MotPhan"))
            {
                label2.Text = label2.Text.Insert(label2.Text.IndexOf('M'), "MotPhan("); label2.Text = label2.Text.Insert(label2.Text.IndexOf(')'), ")");
            }
            else label2.Text = label2.Text + "MotPhan(" + label1.Text + ")";
            label1.Text = (1 / Convert.ToDecimal(label1.Text)).ToString(); clear = 3;
        }
        /// <summary>
        /// Phương thức khi ấn phím Căn 2
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonCan2_Click(object sender, EventArgs e)
        {
            if (label2.Text.Contains("sqrt"))
            {
                label2.Text = label2.Text.Insert(label2.Text.IndexOf('s'), "sqrt("); label2.Text = label2.Text.Insert(label2.Text.IndexOf(')'), ")");
            }
            else label2.Text = label2.Text + "sqrt(" + label1.Text + ")";
            label1.Text = Math.Sqrt(Convert.ToDouble(label1.Text)).ToString(); clear = 4;
        }
        /// <summary>
        /// Phương thức khi ấn phím Phần trăm
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonPhanTram_Click(object sender, EventArgs e)
        {
            label1.Text = (result * Convert.ToDecimal(label1.Text) / 100).ToString();
            label2.Text = label2.Text + label1.Text; clear = 5;
        }
        /// <summary>
        /// Phương thức khi ấn phím Đổi dấu
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonDoiDau_Click(object sender, EventArgs e)
        {
            if (label1.Text.Contains('-'))
            {
                label1.Text = label1.Text.Remove(label1.Text.IndexOf('-'), 1);
            }
            else label1.Text = "-" + label1.Text;
        }
        /// <summary>
        /// Phương thức khi ấn phím Bằng
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonKetQua_Click(object sender, EventArgs e)
        {
            if (clear != 3 && clear != 4 && clear != 5)
                label2.Text = label2.Text + label1.Text;
            label1.Text = eval(op).ToString(); clear = 2;
        }
        /// <summary>
        /// Phương thức khi ấn phím CE
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonCE_Click(object sender, EventArgs e)
        {
            Clear(2); clear = 1; label1.Text = "0"; result = 0;
        }
        /// <summary>
        /// Phương thức khi ấn phím C
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonC_Click(object sender, EventArgs e)
        {
            Clear(1); clear = 1; label1.Text = "0";
        }
        /// <summary>
        /// Phương thức khi ấn phím MS
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonMS_Click(object sender, EventArgs e)
        {
            mresult = Convert.ToDecimal(label1.Text); clear = 1; label3.Text = "M";
        }
        /// <summary>
        /// Phương thức khi ấn phím MR
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonMR_Click(object sender, EventArgs e)
        {
            label1.Text = mresult.ToString();
        }
        /// <summary>
        /// Phương thức khi ấn phím M cộng
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonMcong_Click(object sender, EventArgs e)
        {
            mresult = mresult + Convert.ToDecimal(label1.Text); clear = 1;
        }
        /// <summary>
        /// Phương thức khi ấn phím M trừ
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonMtru_Click(object sender, EventArgs e)
        {
            mresult = mresult - Convert.ToDecimal(label1.Text); clear = 1;
        }
        /// <summary>
        /// Phương thức khi ấn phím MC
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonMC_Click(object sender, EventArgs e)
        {
            mresult = 0; clear = 1; label3.Text = "";
        }
    }
}
